import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {generatePageInfo} from '../../util';
import {movieKeys, searchResult} from '../search-list/search-list.component';

@Injectable({
  providedIn: 'root'
})
export class SearchService {


  constructor(private http: HttpClient) {

  }

  public search(queryStr: string) {
    this.http.get(`/solr/movie/search?k=${queryStr}&size=12`, {observe: 'response'}).subscribe(result => {
      generatePageInfo(searchResult, result);
    });
  }

  public analyzeField(field: string) {
    this.http.get('/solr/movie/analysis?field=' + field).subscribe(result => {
      movieKeys.splice(0, movieKeys.length);
      for (const i in result) {
        if (i) {
          const key: string = result[i];
          if (key.length <= 1) {
            continue;
          }
          movieKeys.push(key);
        }
      }
    });
  }

}
